home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / nihcl-30.lha / nihcl-3.0 / lib / KeySortCltn.c < prev    next >
C/C++ Source or Header  |  1990-05-19  |  3KB  |  163 lines

  1. /* KeySortCltn.c  -- implementation of class KeySortCltn
  2.  
  3.     THIS SOFTWARE FITS THE DESCRIPTION IN THE U.S. COPYRIGHT ACT OF A
  4.     "UNITED STATES GOVERNMENT WORK".  IT WAS WRITTEN AS A PART OF THE
  5.     AUTHOR'S OFFICIAL DUTIES AS A GOVERNMENT EMPLOYEE.  THIS MEANS IT
  6.     CANNOT BE COPYRIGHTED.  THIS SOFTWARE IS FREELY AVAILABLE TO THE
  7.     PUBLIC FOR USE WITHOUT A COPYRIGHT NOTICE, AND THERE ARE NO
  8.     RESTRICTIONS ON ITS USE, NOW OR SUBSEQUENTLY.
  9.  
  10. Author:
  11.     S. M. Orlow
  12.     Systex Inc.
  13.     Beltsville, MD 20705
  14.  
  15. Contractor:
  16.     K. E. Gorlen
  17.     Bg. 12A, Rm. 2033
  18.     Computer Systems Laboratory
  19.     Division of Computer Research and Technology
  20.     National Institutes of Health
  21.     Bethesda, Maryland 20892
  22.     Phone: (301) 496-1111
  23.     uucp: uunet!nih-csl!kgorlen
  24.     Internet: kgorlen@alw.nih.gov
  25.     February, 1987
  26.  
  27. Function:
  28.     
  29. Modification History:
  30.  
  31. $Log:    KeySortCltn.c,v $
  32.  * Revision 3.0  90/05/20  00:19:58  kgorlen
  33.  * Release for 1st edition.
  34.  * 
  35. */
  36.  
  37. #include "nihclIO.h"
  38. #include "Assoc.h"
  39. #include "KeySortCltn.h"
  40.  
  41. #define    THIS    KeySortCltn
  42. #define    BASE    SortedCltn
  43. #define BASE_CLASSES BASE::desc()
  44. #define MEMBER_CLASSES
  45. #define VIRTUAL_BASE_CLASSES
  46.  
  47. DEFINE_CLASS(KeySortCltn,1,"$Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/lib/RCS/KeySortCltn.c,v 3.0 90/05/20 00:19:58 kgorlen Rel $",NULL,NULL);
  48.  
  49. KeySortCltn::KeySortCltn(int size) : BASE(size) {}
  50.  
  51. #ifndef BUG_TOOBIG
  52. // yacc stack overflow
  53. KeySortCltn::KeySortCltn(const KeySortCltn& c) : BASE(c) {}
  54. #endif
  55.  
  56. LookupKey* KeySortCltn::assocAt(int i)
  57. {
  58.     return LookupKey::castdown(at(i));
  59. }
  60.  
  61. Object* KeySortCltn::keyAt(int i)
  62. {
  63.     LookupKey& assoc = *assocAt(i);
  64.     return assoc.key();
  65. }
  66.  
  67. Object* KeySortCltn::valueAt(int i)
  68. {
  69.     LookupKey& assoc = *assocAt(i);
  70.     return assoc.value();
  71. }
  72.  
  73. Object* KeySortCltn::atKey(Object& key)
  74. {
  75.     // binary search for key
  76.     int i = findIndexOf(key);
  77.  
  78.     if (i > -1 && key.compare(*keyAt(i)) == 0)
  79.         return valueAt(i);
  80.     else 
  81.         return nil; // key not found
  82. }
  83.  
  84. Assoc* KeySortCltn::addAssoc(Object& key, Object& val)
  85. {
  86.     Assoc* assoc = new Assoc(key,val);
  87.     SortedCltn::add(*assoc);
  88.     return assoc;
  89. }
  90.  
  91. Collection& KeySortCltn::addValuesTo(Collection& c) const
  92. {
  93.     DO(*this,LookupKey,as) c.add(*(as->value())); OD
  94.     return c;
  95. }
  96.  
  97. Collection& KeySortCltn::addKeysTo(Collection& c) const
  98. {
  99.     DO(*this,LookupKey,as) c.add(*(as->key())); OD
  100.     return c;
  101. }
  102.  
  103. Collection& KeySortCltn::addContentsBeforeTo(Object& key,Collection& c)
  104. {
  105.     if (isEmpty()) return c;
  106.     int k = findIndexOf(key);
  107.     if (k<0) return c;
  108.     for (int i=0; i<=k; i++ )
  109.         c.add(*at(i));
  110.     return c;
  111. }
  112.  
  113. void KeySortCltn::deepenShallowCopy()
  114. {
  115.     BASE::deepenShallowCopy();
  116. }
  117.  
  118. int KeySortCltn::occurrencesOfKey(const Object& key) const
  119. {
  120.     return SortedCltn::occurrencesOf(LookupKey((Object&)key));
  121. }
  122.  
  123. bool KeySortCltn::includesKey(Object& key) const
  124. {
  125.     return occurrencesOfKey(key) > 0;
  126. }
  127.  
  128. Object* KeySortCltn::removeKey(Object& key) { return SortedCltn::remove(LookupKey(key)); }
  129.  
  130. int KeySortCltn::findIndexOf(Object& key) const
  131. /*
  132.     if key is found return index of key
  133.     else if key is before all keys return -1
  134.         else return index of last key before given key
  135. */
  136. {
  137.     return SortedCltn::findIndexOf(LookupKey(key));
  138. }
  139.  
  140. Range KeySortCltn::findRangeOfKey(Object& key) const
  141. {
  142.     return SortedCltn::findRangeOf(LookupKey(key));
  143. }
  144.  
  145. KeySortCltn::KeySortCltn(OIOin& strm)
  146. :
  147. #ifdef MI
  148.     Object(strm),
  149. #endif
  150.     BASE(strm)
  151. {
  152. }
  153.  
  154. KeySortCltn::KeySortCltn(OIOifd& fd)
  155. :
  156. #ifdef MI
  157.     Object(fd),
  158. #endif
  159.     BASE(fd)
  160.  
  161. {
  162. }
  163.